package com.szh.task;

import com.szh.domain.OrderInfo;
import com.szh.enums.PayType;
import com.szh.service.AliPayService;
import com.szh.service.OrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 */
@Component
public class AliPayTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(AliPayTask.class);

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private AliPayService aliPayService;

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过1分钟、并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm() {
        LOGGER.info("orderConfirm定时任务被执行....");
        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayType.ALIPAY.getType());
        orderInfoList.forEach(order -> {
            String orderNo = order.getOrderNo();
            LOGGER.warn("超时订单 ===> {}", orderNo);
            //核实订单状态，调用支付宝查单接口
            aliPayService.checkOrderStatus(orderNo);
        });
    }
}
